# = test.files.3.rb - My Files , Add and Review File Test
# 
# See LICENSE.txt for licensing details.
#
# == Description
# Here the functionalitied a) uploading a file , b) verifying  the uploaded file  and  then c) review of  the uploaded file are tested
require 'watir'
require 'watir/WindowHelper'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'

class TC_FileGalleryAddAndReviewFile_test < Test::Unit::TestCase
    
    def startup			
      $PostSubject = randomChar(20)
      $PostBody = randomChar(50)
      $FilePath = build_windows_path('resources/gallery_slideshow.gif')
      @@ie = Watir::IE.start($SITE_URL)
    end
  
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end

    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
      assert(ie.link(:text, /Control Panel/).exists?)
      ie.link(:text, /Control Panel/).click
      assert(ie.contains_text(/Community Server Announcements/))      
    end
    
    def test_07_verifyControlPanelTabBarLinks
      assert_equal $SITE_URL + 'controlpanel/default.aspx', ie.link(:text, 'Dashboard').href
      assert_equal $SITE_URL + 'controlpanel/blogs', ie.link(:text, 'My Blogs').href
      assert_equal $SITE_URL + 'controlpanel/photos/', ie.link(:text, 'My Photos').href
      assert_equal $SITE_URL + 'controlpanel/files', ie.link(:text, 'My Files').href
      assert_equal $SITE_URL + 'controlpanel/moderation', ie.link(:text, 'Forums Moderation').href
      assert_equal $SITE_URL + 'controlpanel/Setup.aspx', ie.link(:text, 'Administration').href
      assert_equal $SITE_URL + 'controlpanel/Reporting/default.aspx', ie.link(:text, 'Reporting').href
    end
    
    def test_08_navigateToMyFilesDashboard
      ie.link(:text, 'My Files').click
      assert(ie.contains_text("My Files Dashboard"))
      assert(ie.contains_text("Current Folder"))   
    end
    
    def test_09_navigateToAddPost
      ie.goto($SITE_URL + 'ControlPanel/Files/fileeditor.aspx?SelectedNavItem=NewFile')
      assert(ie.contains_text("Upload File"))
    end
    
    def test_10_verifyLinksExistOnUploadFilePage
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert(ie.link(:text, 'Dashboard').exists?)
      assert(ie.link(:text, 'My Blogs').exists?)
      assert(ie.link(:text, 'My Photos').exists?)
      assert(ie.link(:text, 'My Files').exists?)
      assert(ie.link(:text, 'Forums Moderation').exists?)
      assert(ie.link(:text, 'Administration').exists?)
      assert(ie.link(:text, 'Reporting').exists?)
      assert(ie.link(:id, /UploadFile$/).exists?)
      assert(ie.link(:id, /Helpicon1$/).exists?)
      assert(ie.link(:id, /Helpicon4$/).exists?)
      assert(ie.link(:id, /PostButton$/).exists?)
      assert(ie.link(:id, /EulaHyperlink$/).exists?)
      assert(ie.link(:text, 'Telligent').exists?)
    end
    
    def test_11_verifyFieldsExistOnUploadFilePage
      assert(ie.radio(:id, /IsApproved_0$/).exists?)
      assert(ie.radio(:id, /IsApproved_1$/).exists?)
      assert(ie.text_field(:id, /TaskRegion_PostSubject$/).exists?)
      assert(ie.text_field(:id, /_Tags$/).exists?)
    end
    
    def test_12_uploadFile
      assert(ie.link(:text, 'Pick File').exists?)
      ie.link(:text, 'Pick File').click
      
      sleep 5 
      assert(ie.frame(:index, 4).fileField(:name, /File$/).exists?)
      assert(ie.frame(:index, 4).link(:text, 'Save').exists?)
      assert(ie.frame(:index, 4).radio(:name, /UploadOption$/).exists?)
      assert(ie.frame(:index, 4).radio(:id, /LinkToUrl$/).exists?)
      
      ie.frame(:index, 4).fileField(:name, /File$/).set($FilePath)
      
      ie.frame(:index, 4).link(:text, 'Save').click
      sleep 5
      ie.text_field(:id, /PostSubject$/).value = $PostSubject
      ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + $PostBody + "')")
      ie.link(:text, 'Save').click
      sleep 3
    end
    
    def test_13_verifyFileUploadedSuccessfuly
      assert(ie.contains_text('Files'))
      assert(ie.link(:text, $PostSubject).exists?)
    end
    
    def test_14_verifyActionsOnFileListPage
      assert(ie.link(:text, 'Edit').exists?)
      assert(ie.link(:text, 'Delete').exists?)
      assert(ie.link(:text, 'View').exists?)
    end
    
    def test_15_reviewUploadedFile
      ie.link(:text, $PostSubject).click
      assert(ie.contains_text('Upload File'))
      assert(ie.text_field(:id, /PostSubject$/).exists?)
      sleep 3
      assert(ie.cell(:text, 'Options').exists?)
      assert(ie.cell(:text, 'Preview').exists?)
      assert(ie.cell(:text, 'File').exists?)
    end
    
    def test_16_unpublishFile
      ie.cell(:text, 'Options').click
      sleep 3 
      assert(ie.radio(:id, /IsApproved_0$/).exists?)
      assert(ie.radio(:id, /IsApproved_1$/).exists?)
      ie.radio(:id, /IsApproved_1$/).set
      
      assert(ie.link(:text, 'Save').exists?)
      ie.link(:text, 'Save').click
      sleep 3 
      assert(ie.contains_text('Files'))
      assert(ie.image(:src, $SITE_URL + 'controlpanel/images/false.gif').exists?)
    end

end